package com.jxd.hbase.utils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseUtils {

    private static Connection conn = null;
    private static Table table = null;

    private static final String LINE_TABLE = "line";
    public static final String FAMILY = "f1";
    public static final String MES_SIGN_ID = "mesSignID";
    public static final String MES_DATE = "mesDate";
    public static final String MES_ID = "mesID";
    public static final String MES_TYPE = "mesType";
    public static final String MES_SIZE = "mesSize";
    public static final String MES_SEQ = "mesSeq";
    public static final String MES_TIME = "mesTime";
    public static final String SEND_DEVICE = "sendDevice";
    public static final String RECEIVE_DEVICE = "receiveDevice";
    public static final String MARK = "mark";
    public static final String FILENAME = "fileName";
    public static final String SHIP_ID = "shipId";

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static Calendar calendar = Calendar.getInstance();

    static {
        try {
            conn = ConnectionFactory.createConnection();
            TableName tableName = TableName.valueOf(LINE_TABLE);
            table = conn.getTable(tableName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 根据指定rowKey获取单条数据
     *
     * @param rowKey
     */
    public static Result getLineByRowKey(String rowKey) throws IOException {
        Get get = new Get(rowKey.getBytes());
        return table.get(get);
    }

    /**
     * 指定rowKey范围查询数据
     *
     * @param startKey 开始key
     * @param endKey    结束key
     * @throws IOException
     */
    public static Iterator<Result> getLineByStartKeyAndEndKey(String startKey, String endKey) throws IOException {
        Scan scan = new Scan();
        scan.setStartRow(startKey.getBytes());
        scan.setStopRow(endKey.getBytes());
        ResultScanner scanner = table.getScanner(scan);
        Iterator<Result> iterator = scanner.iterator();
        return iterator;

    }


    /**
     * 根据传入的日期字符串返回在此日期之前或者之后秒数的日期字符串
     *
     * @param strDate 传入的日期字符串
     * @param seconds 改变的秒数 ， 正数 表示在此时间之后， 负数 表示在此时间之前
     * @return
     * @throws ParseException
     */
    public static String getChangedStrDate(String strDate, int seconds) throws ParseException {
        calendar.setTime(sdf.parse(strDate));
        calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + seconds);
        return sdf.format(calendar.getTime());
    }

    /**
     * 通过轮船id和发送设备名称得到发送设备代码
     * @param shipID
     * @param sendDeviceName
     * @return
     */
    public static String getSendDeviceCodeByShipIDAndSendDeviceName(String shipID, String sendDeviceName) {

        // 1.通过shipId得到船的型号
        return "";
    }

    /**
     * 关闭资源，应该所有的指标都分析完再调用此方法。
     * @throws IOException
     */
    public static void close() throws IOException {
        table.close();
        conn.close();
    }

    public static void main(String[] args) throws IOException {
//        Result rs = getLineByRowKey("1,20200801.data,123,41040,2020/08/02 05:43:25");
//        String rowKey = Bytes.toString(rs.getRow());
//        System.out.println(rowKey);

        Iterator<Result> iterator = getLineByStartKeyAndEndKey("1,20200801.data,123,61616,2020/08/03 20:30:39",
                "1,20200801.data,123,61616,2020/08/03 20:39:39");
        while (iterator.hasNext()) {
            Result rs = iterator.next();
            String rowKey = Bytes.toString(rs.getRow());
            String missileType = Bytes.toString(rs.getValue(Bytes.toBytes(HBaseUtils.FAMILY), Bytes.toBytes("19")));
            System.out.println(rowKey + "---------->" + missileType);
        }
    }
}
